package jetbrains.buildServer.cuta.logic.operators;

import jetbrains.buildServer.cuta.logic.*;
import jetbrains.buildServer.cuta.logic.sheet.*;
import org.apache.commons.beanutils.BeanUtils;
import org.jdom.Element;
import org.jetbrains.annotations.NotNull;

import static jetbrains.buildServer.cuta.utils.DomUtil.*;


/**
 * @author Leonid Bushuev from JetBrains
 */

class GetPropertyOperator extends AbstractOperator
{


    private String myPropertyName = "";


    @Override
    public void init(Element element, ColumnDef column, ExternalContext extContext)
    {
        myPropertyName = getAttrString(element, "name", "");
    }


    @Override
    public void run(Object object, Cell cell)
    {
        String value;

        try
        {
            value = BeanUtils.getProperty(object, myPropertyName);
        }
        catch (Exception e)
        {
            value = "Error: " + e.getMessage();
        }

        cell.addText(value);
    }


    @Override
    @NotNull
    public String name()
    {
        return "get-property";
    }


    @Override
    public String toString()
    {
        return name() + " " + myPropertyName;
    }
}
